package com.relsell.utility;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Set;

import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;

public class NetworkUtil {

	private static NetworkUtil snetworkUtil;
	private String mServerName = "";
	private String mServerPort = "";
	private boolean mIsLocal = false;

	public static NetworkUtil getInstance() {
		if (snetworkUtil == null) {
			snetworkUtil = new NetworkUtil();
		}
		return snetworkUtil;
	}

	public String getServerName() {
		if (mIsLocal) {
			mServerName = "192.168.1.5";
		} else {
			mServerName = "khottal.info";
		}
		return mServerName;
	}

	public void setServerName(String mServerName) {
		this.mServerName = mServerName;
	}

	public String getServerPort() {
		if (mIsLocal) {
			mServerPort = "9093";
		} else {
			mServerPort = "80";
		}
		return mServerPort;
	}

	public void setServerPort(String mServerPort) {
		this.mServerPort = mServerPort;
	}

	public String convertInputStreamToString(InputStream inputStream)
			throws IOException {
		BufferedReader bufferedReader = new BufferedReader(
				new InputStreamReader(inputStream));
		String line = "";
		String result = "";
		while ((line = bufferedReader.readLine()) != null)
			result += line;

		inputStream.close();
		return result;

	}

	public String makeURL() {
		String url = null;
		url = "http://" + getServerName() + ":" + getServerPort()
				+ "/gcminteraction";
		return url;
	}

	public String makeURLOptions(int pageOption) {
		String urlOptions = null;
		urlOptions = "/view/startpoc.php?controlVar=" + pageOption;
		return urlOptions;
	}

	public InputStream postData(HashMap<String, String> hm, int pageOption) {
		String url = makeURL() + makeURLOptions(pageOption);
		InputStream inputStream = null;
		// Create a new HttpClient and Post Header
		HttpClient httpclient = new DefaultHttpClient();
		HttpPost httppost = new HttpPost(url);

		try {
			// Add your data
			List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();

			Set<String> keySet = hm != null && hm.size() != 0 ? hm.keySet()
					: null;

			if (keySet != null) {
				for (String key : keySet) {
					nameValuePairs
							.add(new BasicNameValuePair(key, hm.get(key)));

					httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));

				}
			}

			// Execute HTTP Post Request
			HttpResponse response = httpclient.execute(httppost);

			inputStream = response.getEntity().getContent();

		} catch (ClientProtocolException e) {
			return null;
			// TODO Auto-generated catch block
		} catch (IOException e) {
			// TODO Auto-generated catch block
			return null;
		}
		return inputStream;
	}
}
